<template>
	<view class="page_body">
		<!-- 状态栏高度 设置背景色 占位状态栏-->
		<view class="ztl"
			:style="{'height':statusBarHeight}"></view>
		<!-- 占位盒子 -->
		<view class=""
			:style="{'height': hieght}"></view>
		<!-- 导航栏 -->
		<navBar bgColor="#FFF">
			<image :slot="'left'"
				@click="navToBack"
				:src="xcxImgUrl && xcxImgUrl+'fanhui.svg'"
				style="width: 36rpx;height: 36rpx;"></image>
			<text :slot="'content'">收货地址</text>
		</navBar>
		
		<scroll-view 
		class="main_harvestAddress pd_lr10" 
		scroll-y
		@scrolltolower="scrolltolower"
		:style="{'height': 'calc(100vh - ' + demoTopHieght +')'}">
			<view class="main_addressList mt10" v-for="(item,index) in addressList" :key="index">
				<view class="main_addressList_item pd10">
					<view class="main_addressList_item_top">
						<view class="main_addressList_item_top_zi">
							<view>{{item.name ? item.name.substr(0,1) : '默'}}</view>
						</view>
						<view class="main_addressList_item_top_cn ml5">
							<view class="main_addressList_item_top_cn_top textOneShow">
								<text class="cn_top_name ">{{item.name ? item.name : ''}}</text>
								<text class="cn_top_phone ml5">{{item.phone ? item.phone : ''}}</text>
							</view>
							<view class="main_addressList_item_top_cn_btn mt10 texttowShow">
								 {{item.province ? item.province : ''}}
								 {{item.city ? item.city : ''}}
								 {{item.area ? item.area : '' }}
								 {{item.address ? item.address : '' }}
							</view>
						</view>
						<view  
						@click="updAddress(item)"
						class="main_addressList_item_top_right">
							<image mode="aspectFill" :src="xcxImgUrl && xcxImgUrl+'page_setting/static/bianjidizhi.svg'"></image>
						</view>
					</view>
					<view class="main_addressList_item_bot">
						<view 
						@click="settingMorenAddress(item)"
						class="main_addressList_item_bot_left">
							<radio 
							color="#ec4141" 
							style="transform:scale(0.7)" 
							:checked="item.type == 1 ? true : false"
							/>设置为默认地址
						</view>
						<view 
						@click="delectAddress(item)"
						class="main_addressList_item_bot_right">
							删除
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		
		<view class="main_btn" @click="addAddress">
			<newBtn name="新增地址" bg-color="#F33340" color="#FFF" ></newBtn>
		</view>
		
		<!-- 模态框确认 -->
		<u-modal 
		:show="modalShow" 
		:title="modalInfo.title" 
		showConfirmButton
		showCancelButton
		@confirm='modalConfirm'
		@cancel='modalShow = false'
		:content='modalInfo.content'>
		
		</u-modal>
	</view>
</template>

<script>
	import {mapGetters} from 'vuex'
	import newBtn from '@/components/newBtn/newBtn.vue'
	import {listMallCustomerAddressPageInfo,
	setDefaultMallCustomerAddress,
	delMallCustomerAddress} from '@/api/setting.js'
	export default {
		computed: {
			...mapGetters(['demoTopHieght']),
		},
		components:{
			newBtn
		},
		data() {
			return {
				statusBarHeight:'',
				hieght:'',
				modalShow:false,//模态框显示
				modalInfo:{//模态框显示内容
					title:'',
					content:''
				},
				pageInfo:{//分页信息
					currentPage:1,//当前页码[默认为1]
					pageSize:10,//每页显示条数[默认为20]
					sort:1
				},
				pages:'',//总页数
				addressList:[],//地址列表
				addressItem:{},//当前选择的地址对象
				routeType:'',//来本页面之前的页面
				isLoad:false
			};
		},
		onShow() {
			this.$nextTick(() => {
				// 获取状态栏的高度
				this.statusBarHeight = this.$store.state.statusBarHeight * 2 + 'rpx'
				// 计算站位的高度
				this.hieght = (this.$store.state.navBarHeight) * 2 + 'rpx'
				//获取高度
				this.Utils.getDemoTopHeight('main_harvestAddress')
			})
			//获取列表
			this.pageInfo.currentPage = 1
			this.addressList = []
			this.getList()
		},
		onLoad(options) {
			this.routeType = options?.type || ''
			this.isLoad = true;
			this.pageInfo.currentPage = 1
			this.addressList = []
			this.getList()
		},
		methods:{
			//返回上一级
			navToBack(){
				if(this.routeType == 'myde'){
					uni.switchTab({
						url:'/pages/myde/myde'
					})
				}
				if(this.routeType == 'settingHome'){
					uni.navigateTo({
						url:'/page_setting/settingHome'
					})
				}
				if(this.routeType == 'address'){
					uni.navigateBack()
				}
			},
			//新增地址
			addAddress(){
				uni.navigateTo({
					url:'/page_setting/addAddress'
				})
			},
			//分页加载
			scrolltolower(){
				++this.pageInfo.currentPage
				if(this.pageInfo.currentPage > this.pages){
					this.Utils.toast('数据加载完啦')
					return
				}
				this.getList()
			},
			//获取收货地址
			async getList(){
				uni.showLoading()
				let res = await listMallCustomerAddressPageInfo(this.pageInfo)
				console.log(res);
				if(res.code == 2000){
					
					this.pages = res.data && 
					res.data.info && 
					res.data.info.pages || 0;
					if(this.pageInfo.currentPage == 1){
						this.addressList = res.data && 
					res.data.info && 
					res.data.info.list;
					}else{
						this.addressList = res.data && 
					res.data.info && 
					res.data.info.list && 
					res.data.info.list.length ? 
					this.addressList.concat(res.data.info.list) : [];
					}
					uni.hideLoading()
				}else{
					this.Utils.toast(`${res.msg}`)
				}
			},
			//设置默认地址
			async settingMorenAddress(item){
				console.log(item);
				if(item.type == 1)return;
				//设置当前点击为勾选 取消其他勾选
				this.addressList.forEach(it=>{
					if(item.addressId == it.addressId){
						it.type = 1
					}else{
						it.type = 2
					}
				})
				//调用接口
				let res = await setDefaultMallCustomerAddress({
					addressId:item.addressId
				})
				if(res.code == 2000){
					this.Utils.toast('设置成功')
				}else{
					this.Utils.toast(`${res.msg}`)
				}
			},
			//删除地址
			delectAddress(item){
				console.log(item);
				this.addressItem = item
				this.modalShow = true
				this.modalInfo.title = '温馨提示'
				this.modalInfo.content = '是否删除此收货地址，删除后将不可恢复，是否继续？'
			},
			//模态框确认
			async modalConfirm(){
				let res = await delMallCustomerAddress({addressId:this.addressItem.addressId})
				if(res.code == 2000){
					this.Utils.toast('删除成功')
					this.modalShow = false
					this.pageInfo.currentPage = 1
					this.addressList = []
					this.getList()
				}else{
					this.Utils.toast(`${res.msg}`)
				}
			},
			//编辑地址
			updAddress(item){
				console.log(item);
				uni.navigateTo({
					url:`/page_setting/addAddress?addressItem=${encodeURIComponent(JSON.stringify(item)) }`
				})
			}
		}
		
	}
</script>

<style lang="scss" scoped>
.ztl{
	background-color: #FFF;
}

.main_harvestAddress{
	width: 100%;
	box-sizing: border-box;
	padding-bottom: 120rpx;
	.main_addressList{
		width: 100%;
		height: 280rpx;
		background-color: #FFF;
		border-radius: $border_radius_10;
		margin-top: 32rpx;
		.main_addressList_item{
			border-radius: 20rpx;
			padding-top: 15px;
			.main_addressList_item_top{
				display: flex;
				.main_addressList_item_top_zi{
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;
					background-color: #FFE3E3;
					color: $text_color_ec4141;
					text-align: center;
					line-height: 80rpx;
					
				}
				.main_addressList_item_top_cn{
					flex: 1;
					.main_addressList_item_top_cn_top{
						.cn_top_name{
							color: $text_color_333;
							font-size: 32rpx;
							font-weight: bold;
						}
						.cn_top_phone{
							color: $text_color_808080;
							font-size: $font_size_14;
							font-weight: bold;
						}
					}
					.main_addressList_item_top_cn_btn{
						color: $text_color_333;
						font-size: $font_size_14;
					}
				}
				.main_addressList_item_top_right{
					width: 80rpx;
					display: flex;
					align-items: center;
					justify-content: right;
					image{
						width: 40rpx;
						height: 40rpx;
					}
				}
			}
			.main_addressList_item_bot{
				margin-top: 40rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.main_addressList_item_bot_left{
					color: $text_color_808080;
					font-size: $font_size_14;
				}
				.main_addressList_item_bot_right{
					color: $text_color_333;
					font-size: $font_size_14;
				}
			}
		}
		
	}
}

.main_btn{
	position: fixed;
	left: 0;
	bottom: 40rpx;
	width: 100%;
}
</style>
